package com.fintech.third.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.marvels.common.utils.MarvelsLogUtil;

public class DateUtil {

	/**
	 * 返回时间格式yyyy-MM-dd hh:mm:ss
	 * 
	 * @return
	 */
	public static String dataFormat() {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		return simpleDateFormat.format(new Date());
	}
	
	/**
	 * 按照pattern所指示的样式格式化日期,以String类型返回
	 * 
	 * @param date
	 * @param pattern
	 * @return String
	 */
	public static String format(Date date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}

	/**
	 * 返回时间格式yyyy-MM-dd
	 * 
	 * @return
	 */
	public static String dataFormat3(Date data) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return simpleDateFormat.format(data);
	}

	/**
	 * 返回时间格式yyyyMMddhhmmssSSS
	 * 
	 * @return
	 */
	public static String dateFormat2() {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddhhmmssSSS");
		return simpleDateFormat.format(new Date());
	}

	public static Date strParseToDate(String s) {
		if (StringUtils.isBlank(s)) {
			return null;
		}
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddhhmmss");
		Date date = null;
		Date date2 = null;
		try {
			date = (Date) sdf1.parse(s);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String string = sdf2.format(date);
		try {
			date2 = sdf2.parse(string);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date2;
	}

	public static Date strToDate(String s) throws ParseException {
		Date date = null;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			date = formatter.parse(s);
		} catch (ParseException e) {
			throw e;
		}
		System.out.println(date);
		return date;
	}

	public static Date strToDate2(String s) {
		if (StringUtils.isEmpty(s)) {
			return null;
		}
		Date date = null;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		try {
			date = formatter.parse(s);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(date);
		return date;
	}

	/**
	 * 蜂巢使用
	 * 
	 * @param s
	 * @return
	 */
	public static Date strToDate3(String s) {
		Date date = null;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
		try {
			date = formatter.parse(s);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(date);
		return date;
	}

	public static Date PyStrToDate(String s) {
		if (StringUtils.isEmpty(s)) {
			return null;
		}
		Date date = null;
		SimpleDateFormat formatter = null;
		if (s.length() > 6) {
			if (s.length() == 8) {
				formatter = new SimpleDateFormat("YYYYMMDD");
			} else {
				if (s.contains("-")) {
					formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				} else {
					formatter = new SimpleDateFormat("YYYYMMDD HH:mm:ss");
				}
			}

		} else {
			formatter = new SimpleDateFormat("YYYYMM");
		}
		try {
			date = formatter.parse(s);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(date);
		return date;
	}

	/**
	 * 字符串转日期
	 * 
	 * @param String
	 */
	@SuppressWarnings("unused")
	public static Date StringToDate(String String) {
		MarvelsLogUtil.getInstance().info("转换前类型为：" + String.getClass());
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date time = null;
		try {
			time = format.parse(String);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		MarvelsLogUtil.getInstance().info("转换后类型为：" + time.getClass());
		return time;
	}

	/**
	 * 日期转换成字符串
	 * 
	 * @param date
	 * @return str
	 */
	public static String DateToString(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String string = format.format(date);
		return string;
	}

	/**
	 * 日期转换成字符串
	 * 
	 * @param date
	 * @return str
	 */
	public static String DateToString2(Date date) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		String string = format.format(date);
		return string;
	}

	/**
	 * 日期转日历
	 * 
	 * @param date
	 * @return Calendar
	 */
	public static Calendar DateToCalendar(Date date) {
		MarvelsLogUtil.getInstance().info("转换前类型为：" + date.getClass());
		Calendar startdate = Calendar.getInstance();
		startdate.setTime(date);
		MarvelsLogUtil.getInstance().info("转换后类型为：" + startdate.getClass());
		return startdate;
	}

	/**
	 * 日历转日期
	 * 
	 * @param calendar
	 * @return Date
	 */
	public static Date CalendarToDate(Calendar calendar) {
		MarvelsLogUtil.getInstance().info("转换前类型为：" + calendar.getClass());
		Date date = calendar.getTime();
		MarvelsLogUtil.getInstance().info("转换后类型为：" + date.getClass());
		return date;
	}

	/**
	 * 判断两天相差天数
	 * 
	 * @param fDate
	 * @param oDate
	 * @return
	 */
	public static int daysOfTwo(Date fDate, Date oDate) {

		Calendar aCalendar = Calendar.getInstance();

		aCalendar.setTime(fDate);

		long time1 = aCalendar.getTimeInMillis();

		aCalendar.setTime(oDate);

		long time2 = aCalendar.getTimeInMillis();

		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));

	}

	/**
	 * 返回两个月份之间的所有月份
	 * 
	 * @param minDate
	 * @param maxDate
	 * @return
	 * @throws ParseException
	 */
	public static List<String> getMonthBetween(String minDate, String maxDate) throws ParseException {
		ArrayList<String> result = new ArrayList<String>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");// 格式化为年月

		Calendar min = Calendar.getInstance();
		Calendar max = Calendar.getInstance();

		min.setTime(sdf.parse(minDate));
		min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

		max.setTime(sdf.parse(maxDate));
		max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

		Calendar curr = min;
		while (curr.before(max)) {
			result.add(sdf.format(curr.getTime()));
			curr.add(Calendar.MONTH, 1);
		}

		return result;
	}

	
	
	public static long hoursBetweenTowDate(Date date1, Date date2) throws ParseException {
		long nd = 1000 * 24 * 60 * 60;
		long nh = 1000 * 60 * 60;
		return date2.getDate() - date1.getDate() / nd / nh;
	}

	public static String getLastMonth(int regex){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.MONTH, -regex);
		Date date = c.getTime();
		return df.format(date);
	}
	
	public static String getDay(int regex){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.MONTH, -regex);
		Date date = c.getTime();
		return df.format(date);
	}
	
	
	/**
	 * 计算2个日期之间相差的  相差多少年月日
	 * 比如：2011-02-02 到  2017-03-02 相差 6年，1个月，0天
	 * @param fromDate
	 * @param toDate
	 * @return
	 */
	public static String dayComparePrecise(String dateStr,String formater){
		SimpleDateFormat format = null;
	
		if(formater == null)
			format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		else
			format = new SimpleDateFormat(formater);
		 
        Date date = null;
		try {
			date = format.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
        //开始时间
        Calendar start = Calendar.getInstance();
        start.setTime(date);
        //结束时间
        Calendar end = Calendar.getInstance();
        int subMonthCount = 0;
        if (!start.after(end)){
            subMonthCount = 
                    (end.get(Calendar.YEAR) - start.get(Calendar.YEAR) == 0) 
                            ? end.get(Calendar.MONTH) - start.get(Calendar.MONTH)  //同一年
                    :   ((end.get(Calendar.YEAR) - start.get(Calendar.YEAR) >= 2) //年数差超过2年
                                ? (end.get(Calendar.YEAR) - start.get(Calendar.YEAR) - 1) 
                                    * 12 + start.getActualMaximum(Calendar.MONTH) - start.get(Calendar.MONTH) 
                                    + end.get(Calendar.MONTH) + 1
                                : start.getActualMaximum(Calendar.MONTH) - start.get(Calendar.MONTH) 
                                    + end.get(Calendar.MONTH) + 1);  //年数差为1，Calendar.get(MONTH) 第一月是0，所以+1
	}
        
       String returnStr = "";
       if(subMonthCount >=12)
    	   returnStr = subMonthCount/12+"年";
       if(subMonthCount %12 != 0)
    	   returnStr = returnStr+(subMonthCount%12)+"个月";
       
       return returnStr;
	}
        
	public static void main(String[] args) {
		;
		System.out.println(dayComparePrecise("2016-01-29 12:23:23",null));
	}
	
	public static int getDaysOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

}
